<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;

trait BaseRequestTrait
{
    /**
     * @var string
     */
    private $errorMessage = '';

    /**
     * @var bool
     */
    private $passValidate = true;

    /**
     * @return string
     */
    public function getErrorMessage()
    {
        return $this->errorMessage;
    }

    /**
     * 验证失败时会自动调用
     * @param Validator $validator
     */
    public function failedValidation(Validator $validator)
    {
        $errors = $validator->errors()->all();
        if (!empty($errors)) {
            $this->passValidate = false;
            $this->errorMessage = $errors[0];
        }
    }

    /**
     * @return bool
     */
    public function passValidate()
    {
        return $this->passValidate;
    }
}
